<?php
/**
 * Created by PhpStorm.
 * User: Simpson
 * Date: 2016/9/26
 * Time: 23:15
 */

namespace App\Repository;

use App\Model\Repair;
use Carbon\Carbon;
use App\Service\RepairService;
class RepairRepository
{

    /**
     * @var Repair
     */
    private $repair;

    /**
     * RepairRepository constructor.
     * @param Repair $repair
     */
    public function __construct(Repair $repair)
    {
        $this->repair = $repair;
    }

    public function search($payload = null)
    {
        $query = $this->repair;

        if($payload['operatorId'] ?? null){
            $query = $query->where('operator_id', $payload['operatorId']);
        }

        if(array_key_exists('status', $payload) && $payload['status'] !== ''){
            $query = $query->where('status', $payload['status']);
        }

        return $query->orderBy('created_at', 'desc')->paginate(20);
    }

    public function getRepairers()
    {
        return $this->repair->select(\DB::raw('DISTINCT(operator_id)'))->where('operator_id', '!=', null)->get();
    }

    /**
     * 判断该日期是否超过预约数
     *
     * @param $orderTime
     * @return bool
     */
    public function isOrderOverload($orderTime)
    {
        $time = Carbon::parse($orderTime);

        if(Repair::whereDate('order_time', '=', $time)->count() >= RepairService::REPAIR_COUNT)
            return false;

        return true;
    }

    /**
     * 判断是否有存在未完成预约
     *
     * @param $openId
     * @return bool
     */
    public function isUserHaveUnfinishOrder($openId)
    {
        if(Repair::where('open_id', $openId)->where('status', '<=', 1)->where('status', '>', -1)->first())
            return false;

        return true;
    }

    /**
     * 根据openId查询
     *
     * @param $openId
     * @return mixed
     */
    public function getOrderByOpenId($openId)
    {
        return Repair::where('open_id', $openId)->orderBy('created_at','desc')->get();
    }

    /**
     * 创建预约
     *
     * @param $openId
     * @param $platform
     * @param $description
     * @param $orderTime
     * @param $schedule
     * @return static
     */
    public function addOrder($openId, $platform, $description, $orderTime, $schedule)
    {
        $repair = Repair::create([
            'open_id' => $openId,
            'description' => $description,
            'platform' => $platform,
            'order_time' => $orderTime,
            'schedule_id' => $schedule,
        ]);

        return $repair;
    }

    /**
     * 预约信息API
     *
     * @param $status
     * @param null $operatorId
     * @return mixed
     */
    public function getRepairApi($array)
    {
        $repairs = Repair::where($array)->with('user')->join(
            'schedule', 'schedule_id', '=', 'schedule.id')->orderBy(
            'order_time')->orderBy('schedule.sort')->orderBy(
            'repair_order.created_at')->get(['*', 'repair_order.id as order_id', 'repair_order.created_at as order_created_at']);

        return $repairs;
    }

    public function getTask($array)
    {
        $repairs = Repair::where($array)->with(['operator' => function($query){
            $query->with('user');
        }])->with('user')->join('schedule', 'schedule_id', '=', 'schedule.id')->orderBy(
            'order_time')->orderBy('schedule.sort')->orderBy(
            'repair_order.created_at')->get(['*', 'repair_order.id as order_id', 'repair_order.created_at as order_created_at']);
        return $repairs;
    }

    public function getOrder($id)
    {
        return Repair::find($id);
    }

    /**
     * 维护总数
     *
     * @return int
     */
    public function getCount()
    {
        return $this->repair->count();
    }

    /**
     * 维护某状态总数
     *
     * @param $status
     * @return int
     */
    public function getCountByStatus($status)
    {
        return $this->repair->whereStatus($status)->count();
    }
}